package com.fkjslee.DAO;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author fkjslee
 * @date 2018/4/18 at 16:52
 * Copyright (c) 2018 fkjslee. All rights reserved.
 */

@RestController
public class DBController {

    private static final Logger log = LoggerFactory.getLogger(DBController.class);

    @Autowired
    private AccountRepository accountRepository;

    @RequestMapping(value = "/test")
    public String test() {
        AccountTable accountTable = new AccountTable();
        accountTable.setAccount("1");
        accountTable.setPassword("1");
        accountTable.setNickname("1");
        accountTable.setUserType(AccountTable.UserType.L1);
        accountRepository.save(accountTable);
        return "success";
    }

    @RequestMapping(value = "/loginAccount")
    public String login(HttpServletResponse response, String account, String password) {
        response.addHeader("Access-Control-Allow-Origin", "*");
        AccountTable findUser = accountRepository.findOne(account);
        JSONObject object = new JSONObject();
        if (findUser == null) {
            object.put("type", "fail");
            object.put("code", "1");
            object.put("reason", "用户名不存在");
        } else if (!findUser.getPassword().equals(password)) {
            object.put("type", "fail");
            object.put("code", "2");
            object.put("reason", "密码错误");
        } else {
            object.put("type", "success");
            object.put("code", "0");
        }
        return object.toString();
    }


    @RequestMapping(value = "/registerAccount")
    public String register(HttpServletResponse response, String account, String password) {
        response.addHeader("Access-Control-Allow-Origin", "*");
        log.info("register account, account = {}, password={}", account, password);
        AccountTable findUser = accountRepository.findOne(account);
        if (findUser != null) {
            return "{\"type\":\"fail\", \"code\":\"1\", \"reason\":\"此用户已存在\"}";
        }
        AccountTable newUser = new AccountTable(account, password, "", AccountTable.UserType.L1);
        accountRepository.save(newUser);
        return "{\"type\":\"success\", \"code\":\"0\"}";
    }

    @RequestMapping(value = "/initDatabase")
    public String initDatabase() {
        accountRepository.deleteAll();

        return "initialization database success";
    }

    @RequestMapping(value = "/getMaxTaskSize")
    public String getMaxTaskSize(String userName) {
        AccountTable account = accountRepository.findOne(userName);
        if (account == null) return "{\"type\":\"fail\", \"code\":\"1\", \"reason\": \"无此用户:" + userName + "\"}";
        else return "{\"type\":\"success\", \"code\":\"0\", \"number\":\"" + account.getMaxTaskSize().toString() + "\"}";
    }

    @RequestMapping(value = "/getNeedTime")
    public String getNeedTime(String userName) {
        AccountTable account = accountRepository.findOne(userName);
        if (account == null) return "{\"type\":\"fail\", \"code\":\"1\", \"reason\": \"无此用户:" + userName + "\"}";
        else return "{\"type\":\"success\", \"code\":\"0\", \"needTime\":\"" + account.getNeedTime().toString() + "\"}";
    }

    @RequestMapping(value = "/getRemainTaskSize")
    public String getRemainTaskSize(String userName) {
        AccountTable account = accountRepository.findOne(userName);
        if (account == null) return "{\"type\":\"fail\", \"code\":\"1\", \"reason\": \"无此用户" + userName + "\"}";
        else return "{\"type\":\"success\", \"code\":\"0\", \"number\":\"" + account.getRemainTaskSize().toString() + "\"}";
    }

    @RequestMapping(value = "/decRemainTaskSize")
    @Transactional
    public String decRemainTaskSize(String userName) {
        log.info("dec remain task size user name = {}", userName);
        AccountTable account = accountRepository.findOne(userName);
        if (account == null) return "{\"type\":\"fail\", \"code\":\"1\", \"reason\": \"无此用户" + userName + "\"}";
        account.setRemainTaskSize(account.getRemainTaskSize() - 1);
        accountRepository.save(account);
        return "{\"type\":\"success\", \"code\":\"0\", \"number\":\"" + account.getRemainTaskSize().toString() + "\"}";
    }

    @RequestMapping(value = "/incRemainTaskSize")
    @Transactional
    public String incRemainTaskSize(String userName) {
        log.info("inc remain task size user name = {}", userName);
        AccountTable account = accountRepository.findOne(userName);
        if (account == null) return "{\"type\":\"fail\", \"code\":\"1\", \"reason\": \"无此用户" + userName + "\"}";
        account.setRemainTaskSize(account.getRemainTaskSize() + 1);
        accountRepository.save(account);
        return "{\"type\":\"success\", \"code\":\"0\", \"number\":\"" + account.getRemainTaskSize().toString() + "\"}";
    }

    @RequestMapping(value = "getAccountRunningTask")
    public String getAccountRunningTask(String userName) {
        AccountTable account = accountRepository.findOne(userName);
        if (account == null) return "{\"type\":\"fail\", \"code\":\"1\", \"reason\": \"无此用户" + userName + "\"}";
        Integer number = account.getMaxTaskSize() - account.getRemainTaskSize();
        return "{\"type\":\"success\", \"code\":\"0\", \"number\":\"" + number.toString() + "\"}";
    }

    @RequestMapping(value = "getAccountRemainTask")
    public String getAccountRemainTask(String userName) {
        AccountTable account = accountRepository.findOne(userName);
        if (account == null) return "{\"type\":\"fail\", \"code\":\"1\", \"reason\": \"无此用户" + userName + "\"}";
        Integer number = account.getRemainTaskSize();
        return "{\"type\":\"success\", \"code\":\"0\", \"number\":\"" + number.toString() + "\"}";
    }
}
